// pages/index/index.js
const app = getApp()
import {
  getDays
} from '../../utils/dateTime.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    user_img: '/img/userImg.png',
    teacherId: '',
    classId: '',

    user: {
      userName: '用户名',
    },

    inviteList: [],
    totalPeople: 0,
    totalMoney: 0.0,
    todayPeople: 0,
    todayMoney: 0.0,
    secPeople: 0,
    getDays,
    addShow: false,
    inviteMoney: 0,
    tiShow: false,
    tiIndex: -1,
    tiMoney: 0,
    isFinishUser: false,
    cashNum: 0,
    adduserInfo: {
      name: "",
      tel: ''
    }
  },

  getList() {
    ks.showLoading({ // 显示加载中loading效果 
      title: "加载中",
      mask: true //开启蒙版遮罩
    });
    var that = this
    var url = app.globalData.url + "user/getKsPersonage", user = that.data.user;
    app.wxRequest(url, {
      teacherId: that.data.teacherId
    }, res => {
      console.log(res);
      var datas = res.data.teacher;
      user.userName = datas.teacherName == null ? '用户名' : datas.teacherName
      this.setData({
        teacher: datas,
        user,
        isFinishUser: true
      })
      ks.hideLoading();
      ks.stopPullDownRefresh()
    },
      err => {
        console.log(err);
      })
  },


  // 获取用户邀请人信息
  getInviteList() {
    wx.showLoading({ // 显示加载中loading效果 
      title: "加载中",
      mask: true //开启蒙版遮罩
    });
    var that = this
    var url = app.globalData.url + "main/getDistribution";
    app.wxRequest(url, {
      teacherId: that.data.teacherId,
    }, res => {
      console.log(res);
      var list = res.data.distributions;
      if (list.length != 0) {
        var totalPeople = 0;
        var totalMoney = 0.0;
        var todayPeople = 0;
        var todayMoney = 0.0;
        var allMoney = 0,
          tiMoney = 0;
        for (var i in list) {
          if (list[i].disFlag == 0) {
            allMoney += list[i].getMoney
            totalPeople++;
          } else if (list[i].disFlag == 1) {
            tiMoney += list[i].getMoney
          }
          var num = getDays(res.data.currentTime.split(' ')[0], list[i].disTime.split(' ')[0]);
          console.log(num);
          if (num == 0) {
            if (list[i].disFlag == 0) {
              todayMoney += list[i].getMoney * 1;
              todayPeople++;
            }
          }
        }
        console.log(allMoney);
        console.log(tiMoney);
        totalMoney = allMoney * 1 - tiMoney * 1
        that.setData({
          totalPeople: totalPeople,
          totalMoney: totalMoney.toFixed(2),
          todayPeople: todayPeople,
          todayMoney: todayMoney.toFixed(2),
        })
      }
      wx.hideLoading();
    },
      err => {
        console.log(err);
      })

  },




  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(e);
  },
  // 提交信息
  formSubmit(e) {
    console.log(e);
    var name = e.detail.value.nickname,
      tel = e.detail.value.tel
    console.log(name);
    console.log(tel);

    if (name == '') {
      wx.showToast({
        title: '请填写昵称',
        icon: 'none'
      })
      return
    } else if (!this.validator(tel)) {
      wx.showToast({
        title: '手机号输入有误',
        icon: 'none'
      })
      return
    }


    return
    var that = this;
    var url = app.globalData.url + "nine/updateUsers";
    app.wxRequest(url, {
      userId: that.data.userId,
      str: name,
      tel: tel
    }, res => {
      console.log(res);
      if (res.flag) {
        wx.showToast({
          title: '修改成功',
          icon: 'none'
        })
        var user = that.data.user
        user.userName = name
        that.setData({
          user,
          addShow: false
        })
        this.getList()
      }
    })
  },
  infotapBasicMask() {
    console.log(122);
    this.setData({
      addShow: false
    })
  },


  showhandle() {
    this.setData({
      addShow: true
    })
  },


  tiHandle() {
    console.log(this.data.isFinishUser);
    if (!this.data.isFinishUser) {
      wx.showToast({
        title: '请先完善用户基本信息',
        icon: 'none'
      })
      return
    }
    this.setData({
      tiShow: true
    })
  },


  selectMoney(e) {
    var num = e.currentTarget.dataset.num
    console.log(num);
    if (num > this.data.totalMoney) {
      return wx.showToast({
        title: '账户余额不足',
        icon: 'none'
      })
    }
    this.setData({
      tiMoney: num
    })
  },
  onTiClose() {
    this.setData({
      tiShow: false,
      tiMoney: 0
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(app.globalData.teacherId);
    this.setData({
      teacherId: app.globalData.teacherId,
      // teacherId: 352,
    })
    this.getInviteList()
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  // 验证手机号 和座机号
  validator: function (val) {
    return /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/.test(val) || /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/.test(val);
  },
  // 提现
  toCash() {
    var that = this;
    console.log(that.data.tiMoney);
    if (that.data.tiMoney == 0) {
      return wx.showToast({
        title: '请选择金额',
        icon: 'none'
      })
    }
    var url = app.globalData.url + 'nine/toGetMoney';
    console.log(that.data.user.userName);

    var realTiMoney = (that.data.tiMoney * 0.92).toFixed(2)
    console.log(realTiMoney);

    return

    app.wxRequest(url, {
      teacherId: app.globalData.teacherId,
      openId: app.globalData.openId,
      userName: that.data.user.userName,
      totalMoney: realTiMoney,
    }, res => {
      console.log(res);
      var result = JSON.parse(res.data.datas);
      if (!result.batch_id) {
        wx.showToast({
          title: '失败,' + result.message,
        })
        that.setData({
          tiShow: false
        })
      } else {
        wx.showToast({
          title: '提现成功',
        })
        that.setData({
          totalMoney: that.data.totalMoney - that.data.tiMoney,
        })
        app.wxRequest(app.globalData.url + 'nine/addCashRecords', {
          teacherId: app.globalData.teacherId,
          totalMoney: that.data.tiMoney,
          orderNum: result.out_batch_no,
        }, ress => {


        }, errr => { })
      }


    }, err => { })
  },


})